using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MySceneManager : MonoSingleton<MySceneManager>
{
	private SO_Data_Scene _mData_Scene;

	[HideInInspector]
	public int _NextMapID = 1;

	[HideInInspector]
	public int _Next传送点ID = 1;

	public SO_Data_Scene _Data_Scene
	{
		get
		{
			if (_mData_Scene == null)
			{
				_mData_Scene = MonoSingleton<MyDataManager>.Instance._Data_Scene;
			}
			return _mData_Scene;
		}
	}

	public int _CurrSceneID => _Data_Scene._List_MapName.IndexOf(SceneManager.GetActiveScene().name);

	public event Action _event_触发切换场景 = delegate
	{
	};

	public event Action _event_成功切换场景 = delegate
	{
	};

	public event Action _event_上香或者复活切换场景 = delegate
	{
	};

	public string F_GetCurrSceneName()
	{
		return SceneManager.GetActiveScene().name;
	}

	public int F_GetCurrSceneID()
	{
		return SceneManager.GetActiveScene().buildIndex;
	}

	public Vector2 F_Get_MapPonit_上香(int mapID)
	{
		return _Data_Scene.F_Get_MapPonit_上香(mapID);
	}

	public Vector2 F_Get_MapPonit_传送()
	{
		return F_Get_MapPonit_传送(_NextMapID, _Next传送点ID);
	}

	public Vector2 F_Get_MapPonit_传送(int mapID, int 传送点ID)
	{
		return _Data_Scene.F_Get_MapPonit_传送(mapID, 传送点ID);
	}

	public void F_Get_传送区域_NextMapName(string currMapName, int curr传送点ID, out string nextMapName, out int next传送点ID)
	{
		(int, int) tuple = _Data_Scene.F_GetSceneLink(currMapName, curr传送点ID);
		next传送点ID = tuple.Item2;
		if (next传送点ID == -1)
		{
			nextMapName = "";
			Debug.LogError("啊？114514？？？currMapName=" + currMapName + ",currMapID=-1");
		}
		else
		{
			nextMapName = _Data_Scene._List_MapName[tuple.Item1];
		}
	}

	public void F_通过传送区域切换场景(string nextMapName, int next传送点ID)
	{
		_NextMapID = _Data_Scene._List_MapName.IndexOf(nextMapName);
		_Next传送点ID = next传送点ID;
		Debug.LogError($"_NextMapID={_NextMapID},_Next传送点ID={_Next传送点ID}");
		StartCoroutine(_Cor_切换场景(nextMapName, 是上香: false));
		this._event_触发切换场景();
	}

	public void F_直面天命(string mapName)
	{
		StartCoroutine(_Cor_切换场景(mapName, 是上香: true));
		this._event_触发切换场景();
	}

	private IEnumerator _Cor_切换场景(string nextMapName, bool 是上香)
	{
		AsyncOperation sc = SceneManager.LoadSceneAsync(nextMapName);
		sc.allowSceneActivation = false;
		while (sc.progress < 0.9f || !GameManager._传送动画结束)
		{
			yield return null;
		}
		yield return null;
		sc.allowSceneActivation = true;
		if (是上香)
		{
			this._event_上香或者复活切换场景();
			Debug.LogError("_event_上香或者复活切换场景");
		}
		else
		{
			this._event_成功切换场景();
			Debug.LogError("_event_成功切换场景");
		}
        if (!AdManager.Instance.ShieldAddress) AdManager.Instance.ShowNative();
    }

	public void F_返回主页()
	{
		SceneManager.LoadScene("MainMenu");
	}
}
